package com.xuecheng.content.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import com.xuecheng.commons.constants.CommonConstants;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.constants.MediaConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanWorkDto;
import com.xuecheng.commons.model.dto.WorkDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanWork;
import com.xuecheng.content.mapper.TeachplanWorkMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.content.service.TeachplanWorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.teaching.api.WorkClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-08-14
 */
@Service
public class TeachplanWorkServiceImpl extends ServiceImpl<TeachplanWorkMapper, TeachplanWork> implements TeachplanWorkService {
    @Autowired
    private TeachplanService teachplanService;
    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private WorkClient workClient;

    @Override
    public ResponseResult association(BindTeachplanWorkDto dto) {
        // 1.校验参数合法性
        if (ObjectUtil.isEmpty(dto.getWorkId()) || ObjectUtil.isEmpty(dto.getTeachplanId()) ||ObjectUtil.isEmpty(dto.getWorkTitle())) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        // 2.判断课程状态
        // 2-1 先查课程计划
        Teachplan teachplan = teachplanService.getById(dto.getTeachplanId());
        //设置媒资类型为作业
        teachplan.setMediaType(MediaConstants.WORK);
        teachplanService.updateById(teachplan);
        // 2-2 再查课程信息
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        // 2-3 判断（已提交、已审核、已发布，不允许绑定）
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }

        // 3.feign远程调用媒资微服务
        ResponseResult<WorkVo> responseResult = workClient.findById(dto.getWorkId().longValue());
        // 3-1 判断状态码
        if (responseResult.getCode() != 0) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        // 3-2 取出作业对象
        WorkVo workVo = responseResult.getData();

        //更新
        WorkDto workDto =BeanHelper.copyProperties(workVo,WorkDto.class);
        workDto.setCourseName(courseBase.getName());
        workDto.setWorkId(workVo.getWorkId());
        workDto.setStatus(CommonConstants.USING_FLAG);
        workClient.savework(workDto);

        TeachplanWork teachplanWork = new TeachplanWork();
        teachplanWork.setWorkId(workVo.getWorkId());//作业id
        teachplanWork.setTeachplanId(teachplan.getId());//计划id
        teachplanWork.setCourseId(courseBase.getId());//课程id
        teachplanWork.setCreateDate(new Date());//创建日期
        teachplanWork.setWorkTitle(workVo.getTitle());
        teachplanWork.setCoursePubId(AuthInfoHolder.getCompanyId());
        //teachplanWork.setCreatePeople();//创建人 todo



        // 5.保存
        this.save(teachplanWork);





        // 6.返回ok
        return ResponseResult.okResult();


    }
//作业解绑
    @Override
    public ResponseResult deletework(Long courseBaseId, Long teachplanWorkId) {
        //删除课程计划的作业数据

            //1.检验参数
            if (courseBaseId==null||teachplanWorkId==null) {
                throw new BusinessException(ErrorCode.PARAMSERROR);
            }
            //2.查找课程（根据id）
            CourseBase courseBase = courseBaseService.getById(courseBaseId);
            //3判断课程状态是否可以解除绑定
            String auditStatus = courseBase.getAuditStatus();
            if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                    StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                    StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)) {
                throw new BusinessException(ErrorCode.UPDATEEERROR);
            }
            //4删除
            this.removeById(teachplanWorkId);
            //5.返回
            return ResponseResult.okResult();
        }

    }

